package com.sansui.blog.utils;

import sun.misc.BASE64Decoder;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class base64转换 {
    /**
     * base64字符串转换成图片
     * imgStr base64字符串
     * path 生成图片路径
     * photoName 图片名称
     */

        public static void main(String[] args) {

            String imgStr = "\\x3d\\x3d";
            String path = "C:\\Users\\15783\\Desktop\\pkg";
            String photoName = "111111.png";

            BASE64Decoder decoder = new BASE64Decoder();
            try {
                String baseValue = imgStr.replaceAll(" ", "+");
                byte[] b = decoder.decodeBuffer(baseValue.replace("data:image/jpeg;base64,", ""));
                for (int i = 0; i < b.length; ++i) {
                    if (b[i] < 0) {
                        b[i] += 256;
                    }
                }
                File targetFile = new File(path);
                if(!targetFile.exists()){
                    targetFile.mkdirs();
                }
                OutputStream out = new FileOutputStream(path+"/"+photoName);
                out.write(b);
                out.flush();
                out.close();
                System.out.println("成功");
            } catch (Exception e) {
                System.out.println("失败");
            }
        }

}